﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinqToTwitter;

namespace Version2
{
    class SeattlePDTweetByBeat
    {
        private log4net.ILog logger;
        private List<string> tweetIDs = new List<string>();
        public event EventHandler NewTweet;

        public SeattlePDTweetByBeat()
        {
            tweetIDs = new List<string>();
            logger = log4net.LogManager.GetLogger(typeof(SeattlePDTweetByBeat));
        }

        // This should never be hardcoded! Need to move to an obfuscated properties file or something.
        public SingleUserAuthorizer auth = new SingleUserAuthorizer
        {
            Credentials = new InMemoryCredentials
            {
                //ConsumerKey = @"TFFBLnE35TrHILtrH8wQtQ",
                //ConsumerSecret = @"cU925O6BSTsB9r1M0oTelra2DXrPPAZlTg9V3EieLjE",
                //AccessToken = @"ke2wP0XDBtG1YJFMt5aw44OKfkYegzBRhmEtPBn4",
                //OAuthToken = @"872808954-qdGXOUlGI6KlERDy9d8hLApUl7jG515a0SB88Xmp"

                ConsumerKey = @"lHWBNadh1vLDXdC1aA",
                ConsumerSecret = @"1C5PtF5apuDHvdgdi0CuPxydHKBpNiPgLxlEjTFU",
                AccessToken = @"ZNiNM0dcKcXM9JQjMgBObSH3E65y2HMxC0Xstpo9bk",
                OAuthToken = @"196902335-cXdVrFxn2B61meaPZpTfSnhgGd1uQ7ktkvknkU65"
            }
        };

        public void listenForTweets(){
            logger.Info("Listening for Seattle PD Tweets by Beat ...");
            TwitterContext cntx = new TwitterContext(auth);

            var statusTweets =
             from tweet in cntx.Status
             where tweet.Type == StatusType.User && tweet.CreatedAt > DateTime.Parse("3/6/2013 20:00")
             && (tweet.ScreenName == "SeattlePDB1" || tweet.ScreenName == "SeattlePDB2" || tweet.ScreenName == "SeattlePDB3"
                 || tweet.ScreenName == "SeattlePDN1" || tweet.ScreenName == "SeattlePDN2" || tweet.ScreenName == "SeattlePDN3"
                 || tweet.ScreenName == "SeattlePDJ1" || tweet.ScreenName == "SeattlePDJ2" || tweet.ScreenName == "SeattlePDJ3"
                 || tweet.ScreenName == "SeattlePDU1" || tweet.ScreenName == "SeattlePDU2" || tweet.ScreenName == "SeattlePDU3"
                 || tweet.ScreenName == "SeattlePDL1" || tweet.ScreenName == "SeattlePDL2" || tweet.ScreenName == "SeattlePDL3"
                 || tweet.ScreenName == "SeattlePDQ1" || tweet.ScreenName == "SeattlePDQ2" || tweet.ScreenName == "SeattlePDQ3"
                 || tweet.ScreenName == "SeattlePDD1" || tweet.ScreenName == "SeattlePDD2" || tweet.ScreenName == "SeattlePDD3"
                 || tweet.ScreenName == "SeattlePDM1" || tweet.ScreenName == "SeattlePDM2" || tweet.ScreenName == "SeattlePDM3"
                 || tweet.ScreenName == "SeattlePDK1" || tweet.ScreenName == "SeattlePDK2" || tweet.ScreenName == "SeattlePDK3"
                 || tweet.ScreenName == "SeattlePDO1" || tweet.ScreenName == "SeattlePDO2" || tweet.ScreenName == "SeattlePDO3"
                 || tweet.ScreenName == "SeattlePDR1" || tweet.ScreenName == "SeattlePDR2" || tweet.ScreenName == "SeattlePDR3"
                 || tweet.ScreenName == "SeattlePDS1" || tweet.ScreenName == "SeattlePDS2" || tweet.ScreenName == "SeattlePDS3"
                 || tweet.ScreenName == "SeattlePDE1" || tweet.ScreenName == "SeattlePDE2" || tweet.ScreenName == "SeattlePDE3"
                 || tweet.ScreenName == "SeattlePDC1" || tweet.ScreenName == "SeattlePDC2" || tweet.ScreenName == "SeattlePDC3"
                 || tweet.ScreenName == "SeattlePDG1" || tweet.ScreenName == "SeattlePDG2" || tweet.ScreenName == "SeattlePDG3"
                 || tweet.ScreenName == "SeattlePDW1" || tweet.ScreenName == "SeattlePDW2" || tweet.ScreenName == "SeattlePDW3"
                 || tweet.ScreenName == "SeattlePDF1" || tweet.ScreenName == "SeattlePDF2" || tweet.ScreenName == "SeattlePDF3")
                    select tweet;

            if (statusTweets == null)
                    return;

            foreach (var s in statusTweets)
            {
                if (!tweetIDs.Contains(s.ID))
                {
                    logger.Info("Tweet found " + s.ID + " - " + s.Text);
                    tweetIDs.Add(s.ID);
                    this.NewTweet(s, new EventArgs());
                }
            }
        }

        /// <summary>
        /// Pulls out the address of the Seattle TweetByBeat so that we can geosearch that mofo.
        /// </summary>
        /// <param name="seattleTweet"></param>
        /// <returns></returns>
        public static string ParseAddress(string seattleTweet)
        {
            int startIndex = seattleTweet.IndexOf(" at ");
            if (startIndex == -1)
                return "";

            int endIndex = seattleTweet.IndexOf(" reported on ", startIndex);

            return seattleTweet.Substring(startIndex + 4, endIndex - startIndex - 4).Replace("XX", "00").Replace("BLOCK OF ", "") + ", Seattle, WA";
        }
    }
}
